package gtdz.ui;

import gtdz.algo.rk4.Record;

import java.util.Arrays;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;

public class App extends Application {

	private final TableView<Record> table = new TableView<>();

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		
		// UI
		Scene scene = new Scene(new Group());
		stage.setTitle("graphtool-dz");
		stage.setWidth(300);
		stage.setHeight(500);

		final Label label = new Label("Runge-Kutta 4-th order");
		label.setFont(new Font("Arial", 20));

		table.setEditable(true);

		// Columns.

		Callback<TableColumn<Record, Double>, TableCell<Record, Double>> cellFactory = 
				p -> new EditingCellDouble();
				
		for (String colName : Arrays.asList("v0", "alpha", "step")) {
			TableColumn<Record, Double> col = new TableColumn<>(colName);
			col.setCellValueFactory(new PropertyValueFactory<>(colName));
			col.setCellFactory(cellFactory);
			col.setOnEditCommit(e -> {
				Record r = e.getTableView().getItems().get(e.getTablePosition().getRow()); 
				Double v = e.getNewValue();
				if ("v0".equals(colName)) {
					r.setV0(v);
				} else if ("alpha".equals(colName)) {
					r.setAlpha(v);
				} else if ("alpha".equals(colName)) {
					r.setStep(v);
				}
			});
			col.setMinWidth(100);
			table.getColumns().add(col);
		}
		
		// Table Data.
		ObservableList<Record> data = getData();
		table.setItems(data);

		final Button addButton = new Button("Add");
		addButton.setOnAction(e -> data.add(new Record(300, 30, 0.01))); // new row defaults;

		final Button delButton = new Button ("Delete");
		delButton.setOnAction(e -> { 
			if (!data.isEmpty()) 
				data.remove(table.getSelectionModel().getSelectedIndex());
		}); 

		HBox buttonsBox = new HBox();
		buttonsBox.setSpacing(3);
		buttonsBox.getChildren().addAll(addButton, delButton);
		
		final VBox vbox = new VBox();
		vbox.setSpacing(5);
		vbox.setPadding(new Insets(10, 0, 0, 10)); 
		vbox.getChildren().addAll(label, table, buttonsBox);

		((Group) scene.getRoot()).getChildren().addAll(vbox);

		stage.setScene(scene);
		stage.sizeToScene();
		stage.show();
		
	}

	private ObservableList<Record> getData() {
		return FXCollections.observableArrayList(
				new Record(300, 30, 0.01),
				new Record(300, 30, 0.005), 
				new Record(300, 65, 0.001),
				new Record(300, 65, 0.005), 
				new Record(600, 15, 0.01));
	}
}
